package com.myself.module.authority.mq.consumer.email;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.myself.module.authority.mq.message.email.SendEmailMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author 瑟瑟发抖
 * @version 1.0
 * @description TODO
 * @date 2024/5/20 16:04
 */

@Slf4j
@Component
@RefreshScope
public class SendEmailConsumer {
    @Value("${email.sendHost:smtp.qq.com}")
    String sendHost;
    @Value("${email.sendPort:25}")
    Integer sendPort;

    @Value("${email.user}")
    String sendUser;
    @Value("${email.password}")
    String password;

    @EventListener
    public void onMessage(SendEmailMessage message){
        // 1. 创建发送账号
        MailAccount account =new MailAccount();
        //构建推送参数
        account.setHost(sendHost);
        account.setPort(sendPort);
        account.setAuth(true);
        account.setFrom(sendUser);
        account.setUser(sendUser);
        account.setPass(password);
        String messageId = MailUtil.send(account, message.getMail(),
                message.getTitle(), message.getContent(), true);

    }
}
